<template>
	<view class="box">
		<view class="search" @click="gosearch()">
			<u-search placeholder="搜索" v-model="keyword"></u-search>
		</view>
		<view class="list">
			<scroll-view class="daohang" scroll-y="true">
				<view class="type_item" :class="test===index? 'selectColor':''" v-for="(item,index) in typelist"
					:key="index" @click="selectSnacks(item.type_name);selct(index) ">
					<text>{{item.type_name}}</text>
				</view>
			</scroll-view>

			<scroll-view class="snackslist" scroll-y="true">
				<view class="order">
					<text @click="selectSnacks(type_name);selct2(0)" :class="test2===0? 'selectColor2':''">综合</text>
					<text @click="orderType(type_name,'sales','asc');selct2(1)" :class="test2===1? 'selectColor2':''">销量</text>
					<view class="sort">
					<text @click="orderType(type_name,'price','asc');selct2(2);selct3(0)" :class="test3===0&&test2===2? 'selectColor2':''">价格</text>
					   <view class="sort2">
					   <u-icon name="arrow-up" size="10" @click="orderType(type_name,'price','asc');selct2(2);selct3(0)" :color="test3===0&&test2===2? 'red':''"></u-icon>
					   <u-icon name="arrow-down"  size="10" @click="orderType(type_name,'price','desc');selct2(2);selct3(1)" :color="test3===1&&test2===2? 'red':''"></u-icon>
					   </view>
					</view>
				</view>
				<view v-for="(item,index) in snackList" class="snacksitem">
					<view class="pic">
						<image :src="item.imageValue" mode="aspectFill" @click="godetail(item._id._value)">
					</view>
					<view class="des">
						<view class="desitem1">{{item.name}}</view>
						<view class="desitem2">单个售卖</view>
						<view class="desitem3">
							<text>${{item.price}}</text>
							<view class="click">	
								<u-icon name="plus-circle" size="28" color="red"
									@click="addNumber(user_id,item._id._value,index,1)"></u-icon>
								<u-badge   :value="item._id.shoppingCarts[0].number" offset=[10rpx,10rpx] absolute></u-badge>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>


	</view>
</template>

<script>
	const db=uniCloud.database();
	const dbCmd=db.command;
	const cloudShoppingCarts = uniCloud.importObject("shoppingCarts");
	const cloudType = uniCloud.importObject("type");
	const cloudSnacks = uniCloud.importObject("snacks")
	export default {
		data() {
			return {
				user_id: "",
				keyword: "",
				number: [],
				test: 0,
				test2: 0,
				test3:0,
				typelist: [],
				snackList: [],
				type_name:""
			};
		},
		onLoad() {
			this.user_id = uni.getStorageSync("uni-id-pages-userInfo")._id
			console.log(this.user_id);
			this.getSnack_type()
			this.selectSnacks("无肉不欢")
		},
		onShow() {

		},
		methods: {
			gosearch(){
				uni.navigateTo({
					url:"/pages/searhPage/searhPage"
				})
			},
			selct(index) {
				this.test = index;
			},
            selct2(index) {
            	this.test2 = index;
            },
			selct3(index) {
				this.test3 = index;
			},
			 compare(a, b) {
			    if(a.sales <= b.sales) {
			        return 1;
			    }else
				return -1; 
			},
			async orderType(type_name,orderName,orderType){
				let obj={number:0};
				const a= db.collection("Snacks").where({type:type_name}).orderBy(orderName,orderType).getTemp();
				const b= db.collection("shoppingCarts").where({
					user_id:this.user_id}).field("snack_id,user_id,number").getTemp();
				let res= await db.collection(a,b).get();
				console.log(res.result.data);
				for(let item of res.result.data){
					 if(item._id.shoppingCarts.length==0){
						 item._id.shoppingCarts.push(obj)
					 }
				 }
				this.snackList = res.result.data;
			},
			async addNumber(user_id, snack_id, index,value) {
				this.snackList[index]._id.shoppingCarts[0].number++;
				cloudShoppingCarts.addCartNumber(user_id, snack_id,value).then(res => {
					console.log(res);
				})
			},

			//跳转详情页
			godetail(_id) {
				uni.navigateTo({
					url: `/pages/detail/detail?&_id=${_id}`,
				})
			},

			//获取零食分类
			getSnack_type() {
				cloudType.get().then(res => {
					this.typelist = res.data
				})
			},
			//根据导航搜索零食
		async selectSnacks(type_name) {
		        this.type_name=type_name;
			    let obj={number:0};
				const a= db.collection("Snacks").where({type:type_name}).getTemp();
				const b= db.collection("shoppingCarts").where({
					user_id:this.user_id}).field("snack_id,user_id,number").getTemp();
				let res= await db.collection(a,b).get();
				console.log(res.result.data);
				for(let item of res.result.data){
					 if(item._id.shoppingCarts.length==0){
						 item._id.shoppingCarts.push(obj)
					 }
				 }
				this.snackList = res.result.data;
				console.log(this.snackList);
				// cloudSnacks.selectSnacksBYtype(type_name).then(res => {
				// 	console.log(res);
				// 	this.snackList = res.data;
				// })
			}
		}
	}
</script>

<style lang="scss">
	.box {
		width: var(--status-bar-width);
	}

	.search {
		position: fixed;
		top: var(--window-top, 10rpx);
		height: 60rpx;
		width: 100%;
		z-index: 1;
	}

	.list {
		margin-top: 100rpx;
		display: flex;

		.daohang {
			width: 200rpx;
			display: flex;
			flex-direction: column;
			height: 750px;
			background-color: #f7f7f6;

			.type_item {
				box-sizing: content-box;
				padding-top: 40rpx;
				text-align: center;
				width: 200rpx;
				height: 80rpx;
				// border: 2rpx solid yellow;

			}
		}

		.snackslist {
			margin-left: 20rpx;
			display: flex;
			width: 70%;
			flex-direction: column;
			height: 750px;

			.order {
				height: 60rpx;
				padding: 20rpx;
				display: flex;
				justify-content: space-around;
				text{
					font-size: 40rpx;
				}
				.sort{
					display: flex;
					.sort2{
						display: flex;
						flex-direction: column;
						justify-content: space-around;
						
					}
				}
			}

			.snacksitem {
				box-sizing: content-box;
				width: 100%;
				height: 200rpx;
				border-bottom: 2rpx solid #f7f7f6;
				padding-top: 20rpx;
				display: flex;

				.pic {
					width: 180rpx;
					height: 180rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.des {
					display: flex;
					flex-direction: column;
					margin-left: 10rpx;
					height: 180rpx;
					width: 320rpx;
					justify-content: space-between;

					.desitem1 {
						font-size: 30rpx;
					}

					.desitem2 {
						font-size: 20rpx;
					}

					.desitem3 {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.click {
							display: flex;
							justify-content: space-between;
						}
					}
				}
			}
		}
	}

	.selectColor {
		color: red;
		background-color: white;
		border-left: 2px solid red;
	}
	
	.selectColor2{
		color: red;
	}
</style>